package acwing._1_1AlgorithmBasic._4Math;
/**
 * @Project : AlgorithmLearning
 * @Package : ACWing._1_1AlgorithmBasic._4Math
 * @File : Q0875.java
 * @Author : WangRuoyu
 * @Date : 2023/4/5 9:00
 */

import java.io.*;

public class Q0875 {

    static long quickPower(long a, long b, long p) {
        long res = 1;
        while (b > 0) {
            if ((b & 1) == 1) res = res * a % p;
            b >>= 1;
            a = a * a % p;
        }
        return res;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(System.out));
        String[] words = br.readLine().split(" ");
        int n = Integer.parseInt(words[0]);

        for (int i = 0; i < n; ++i) {
            words = br.readLine().split(" ");
            long a = Long.parseLong(words[0]);
            long b = Long.parseLong(words[1]);
            long p = Long.parseLong(words[2]);
            wr.write(quickPower(a, b, p) + "\n");
        }
        wr.flush();
    }
}